package ph.com.gs3.formalistics.model.communicators;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse;
import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse.APIInvalidResponseException;
import ph.com.gs3.formalistics.model.valueobjects.business.document.Document.DocumentStar;

public class DocumentCommunicatorModule extends HttpCommunicatorModule {

	public static String TAG = DocumentCommunicatorModule.class.getSimpleName();

	public DocumentCommunicatorModule(HttpCommunicator communicator) {
		super(communicator);

	}

	public DocumentCommunicatorModule(HttpCommunicator communicator, String server) {
		super(communicator, server);

	}

	public APIResponse getDocumentStubsToUpdate(String formId, String fromDate)
	        throws CommunicatorException, APIInvalidResponseException {

		commonValidation();

		String url = server + "/API/request-list-new";

		Map<String, String> requestParams = new HashMap<String, String>();

		JSONObject searchParameters = new JSONObject();
		JSONArray fields = new JSONArray();

		fields.put("ID");
		fields.put("DateUpdated");

		try {
			searchParameters.put("fields", fields);

			// add date updated comparison if available
			if (fromDate != null && !fromDate.isEmpty()) {
				JSONObject dateUpdatedConditions = new JSONObject();
				dateUpdatedConditions.put("condition", ">=");
				dateUpdatedConditions.put("compared_to_date", fromDate);

				searchParameters.put("date_updated_comparison", dateUpdatedConditions);

				FLLogger.d(TAG, "Added search parameter, date updated should be >= " + fromDate);

			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		requestParams.put("form_id", formId);		
		requestParams.put("search_parameters", searchParameters.toString());

		FLLogger.d(TAG, "URL: " + url);
		FLLogger.d(TAG, "Params: " + requestParams.toString());

		return getResponse(url, requestParams);

	}

	public APIResponse getDocument(String formId, String documentId) throws CommunicatorException,
	        APIInvalidResponseException {

		commonValidation();

		String url = server + "/API/request";
		Map<String, String> requestParams = new HashMap<String, String>();

		requestParams.put("form_id", formId);
		requestParams.put("request_id", documentId);

		return getResponse(url, requestParams);
	}

	public APIResponse submitDocumentAction(String formId, String requestId,
	        JSONObject requestData, String action) throws CommunicatorException,
	        APIInvalidResponseException {

		commonValidation();

		String url;

		if ("0".equals(requestId.trim())) {
			url = server + "/API/create-request";
		} else {
			url = server + "/API/update-request";
		}

		FLLogger.d(TAG, "submit document action url: " + url);

		Map<String, String> requestParams = new HashMap<String, String>();

		requestParams.put("form_id", formId);
		requestParams.put("request_id", requestId);
		requestParams.put("request_data", requestData.toString());
		requestParams.put("action", action);

		return getResponse(url, requestParams);

	}

	public APIResponse markDocumentStar(String formId, String requestId, DocumentStar star)
	        throws CommunicatorException, APIInvalidResponseException {

		commonValidation();

		String url;

		if (star == DocumentStar.STARRED) {
			url = server + "/API/star-document";
		} else {
			url = server + "/API/unstar-document";
		}

		Map<String, String> requestParams = new HashMap<String, String>();

		requestParams.put("form_id", formId);
		requestParams.put("request_id", requestId);

		return getResponse(url, requestParams);

	}

}
